<?php

namespace controller;

/**
 * kontroler odpowiedzialny za producentów w sklepie
 *
 * @author Damian
 */
class Shop_Manufacturer extends Shop {
    public function index() {
        $manufacturerURL = \library\RequestMethods::get("manufacturerURL");
        $view = $this->getActionView();
        $page = (\library\RequestMethods::get("Page")) ? \library\RequestMethods::get("Page") : 1;
        if (!is_numeric($page)) {
            $page = 1;
        }
        $productsCriteria = new \model\Criteria();

        $layoutView = $this->getLayoutView();
        if ($manufacturerURL) {
            $manufacturer = \model\TblManufacturer::getModel()->getByURL($manufacturerURL, $this->shop->ShopID);
            if ($manufacturer) {
                $session = \library\Registry::get("session");
                $session->set("manufacturer",$manufacturer);
                $layoutView->set("paneltitle", "Produkty producenta " . $manufacturer->Name);
                $productsCriteria=\model\TblProduct::getModel()->createCriteria($this->shop->ShopID)->where(\model\TblProduct::ACTIVE, "=", 1,false," and ")
                        ->where(\model\TblProduct::MANUFACTURER_ID, "=", $manufacturer->ManufacturerID, \model\TblManufacturer::getModel()->getTable(), " and ");
                $view->set("manufacturername", $manufacturer->Name);
            } else {
                $layoutView->set("paneltitle", "Produkty wszystkich producentów ");
                $productsCriteria=\model\TblProduct::getModel()->createCriteria($this->shop->ShopID)->where(\model\TblProduct::ACTIVE, "=", 1,false," and ");                       
            }
        } else {
            $layoutView->set("paneltitle", "Produkty wszystkich producentów ");
            $productsCriteria=\model\TblProduct::getModel()->createCriteria($this->shop->ShopID)->where(\model\TblProduct::ACTIVE, "=", 1,false," and ");  
        }
        $all = $productsCriteria->count();
        if (\library\RequestMethods::get("from") && is_numeric(\library\RequestMethods::get("from"))) {
            $productsCriteria->where(\model\TblProduct::GROSS_PRICE, ">=", \library\RequestMethods::get("from"), \model\TblProduct::getModel()->getTable(), " and ");
            $layoutView->set("from", \library\RequestMethods::get("from"));
        }
        if (\library\RequestMethods::get("to") && is_numeric(\library\RequestMethods::get("to"))) {
            $productsCriteria->where(\model\TblProduct::GROSS_PRICE, "<=", \library\RequestMethods::get("to"), \model\TblProduct::getModel()->getTable(), " and ");
            $layoutView->set("to", \library\RequestMethods::get("to"));
        }
                $database = \library\Registry::get("database");
        /* @var $database \model\database\PDOConnector */
        $maxMinCriteria = new \model\Criteria();
        $maxMinCriteria->from(\model\TblProduct::getModel()->getTable(), ["max(`" . \model\TblProduct::GROSS_PRICE . "`)" => "`max`",
                    "min(`" . \model\TblProduct::GROSS_PRICE . "`)" => "`min`"])
                ->where(\model\TblProduct::VISABLE, "=", 1, false, " ")
                ->where(\model\TblProduct::ACTIVE, "=", 1, false, " and ")
                ->join(\model\TblProductPicture::getModel())
                ->join(\model\TblManufacturer::getModel())
                ->where(\model\TblProductPicture::DEFAULT_PICTURE, "=", 1, \model\TblProductPicture::getModel()->getTable(), " and ");
        if ($manufacturerURL){
            $maxMinCriteria->where(\model\TblManufacturer::URL, "=", $manufacturerURL, \model\TblManufacturer::getModel()->getTable(), " and ");
        }
        $minMax = $database->executeSQL($maxMinCriteria->getSelectQuery())->fetch(\PDO::FETCH_ASSOC);
        $min = floatval($minMax["min"]);
        $max = floatval($minMax["max"]);
        if ($min == $max) {
            $max*=1.1;
            $min = round($min / 1.1, 1);
        }
        if (\library\RequestMethods::get("from") && is_numeric(\library\RequestMethods::get("from"))) {
            $layoutView->set("from", \library\RequestMethods::get("from"));
        } else {
            $layoutView->set("from", $min);
        }
        if (\library\RequestMethods::get("to") && is_numeric(\library\RequestMethods::get("to"))) {
            $layoutView->set("to", \library\RequestMethods::get("to"));
        } else {
            $layoutView->set("to", $max);
        }
        $step = ($max - $min) / 100.0;
        $layoutView->set("min", $min);
        $layoutView->set("max", $max);
         $layoutView->set("step",$step);
        // echo $productsCriteria->limit($this->perPage, $page)->getSelectQuery();
         if (\library\RequestMethods::get("order")){
            $session->set("order",\library\RequestMethods::get("order"));
            }
        $view->set("order",$session->get("order"));
        $order = \model\TblProduct::createOrderString($session->get("order"));
        $productsCriteria->order($order[0], $order[1],  \model\TblProduct::getModel()->getTable());
        $Products = \model\TblProduct::getModel()->all($productsCriteria->limit($this->perPage, $page));
       // var_dump($Products[0]);
       // var_dump($Products);
        $pagesCount = ceil($all / $this->perPage);
        $pages = new \view\Pagination($page, 1, $pagesCount, "./" . $this->shop->URL . "/" . "sm");
      //  var_dump($Products);
        if ($Products) {
            //  var_dump($Products);
            $layoutView->set("pages", $pages);
            
            $view->set("productsPictures", $Products);
        }
        $this->render();
    }
}
